import BaseService from "./BaseService";

/**
 * 购物车服务类
 */
export default class AddressService extends BaseService {
  constructor() {
    super();
  }

  /**
   * 返回分页对象
   */
  list(unionid) {
    const url = `${this.host}/user-addresses?unionid=${unionid}`;
    return this.get(url).then(data => data != '' ? data : Promise.reject('NO_USER_ADDRESS'));
  }

  /**
   * 新增地址
   */
  save(unionid, address) {
    const url = `${this.host}/user-addresses?unionid=${unionid}`;
    return this.post(url, address);
  }

  /**
   * 更新地址对象
   */
  update(unionid, addrId, address) {
    const url = `${this.host}/user-addresses/${addrId}?unionid=${unionid}`;
    return this.put(url, address);
  }

  /**
   * 设置默认
   */
  setDefault(unionid, id) {
    const url = `${this.host}/user-addresses/${id}/default?unionid=${unionid}`;
    return this.put(url);
  }

  /**
   * 获取默认
   */
  getDefault(unionid) {
    const url = `${this.host}/user-addresses/default?unionid=${unionid}`;
    return this.get(url).then(data => data != '' ? data : this.getFirstAddress());
  }

  /**
   * 获取第一个地址
   */
  getFirstAddress(unionid) {
    const url = `${this.host}/user-addresses?unionid=${unionid}`;
    return this.get(url).then(data => data.length > 0 ? data[0] : Promise.reject('NO_ADDRESS'));
  }

  /**
  * 删除地址对象
  */
  remove(id) {
    const url = `${this.host}/user-addresses/${id}`;
    return this.delete(url);
  }

  /**
   * 选择微信地址
   */
  wxAddress() {
    return new Promise((resolve, reject) => {
      wx.chooseAddress({
        success: data => {
          resolve({
            name: data.userName,
            phone: data.telNumber,
            province: data.provinceName,
            city: data.cityName,
            country: data.countyName,
            detail: data.detailInfo,
            isDefault: 0
          });
        },
        fail: reject
      });
    });
  }

  /**
   * 处理地址数据
   */
  _processAddress(data) {
    return data.data;
  }
}